<?php
/**
 * 后台产品管理
 *
 * 后台产品管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\ProductsService\ProductsBackService;
use Illuminate\Support\Facades\Response;
use App\Services\TypesService\TypesBackService;
use App\Services\BrandService\BrandToolService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\SourcePicService\SourcePicApiService;

class ProductsController extends Controller
{
    protected $product_back_service;
    public function __construct(ProductsBackService $product_back_service)
    {
        $this->product_back_service = $product_back_service;
    }

    /**
     * 获取全部作品列表
     * @param Request $request
     * @return $this
     */
    public function getList(Request $request, TypesBackService $typesBackService, BrandToolService $brand_tool_service, ProductTempTool $productTempTool, $type = 1)
    {
        //1 全部 2已上架 3设计师 4品牌商
        $type = intval($type);
        $input = $request->all();
        if(isset($input['conditions']) && !empty($input['conditions'])){
            //有条件查询
            $conditions = json_decode($input['conditions']);
            if(is_null($conditions)){
                return redirect()->back()->with('error_message','查询条件无效');
            }
            $products = $typesBackService->get_search_product_list((array)$conditions);
        }else{
            $products =$this->product_back_service->getList($type,15);
        }
        $brands = $brand_tool_service->getAllBrands();
        $product_temp_categorys = $productTempTool->getAllProductTempCategorys(1);
        return view('backstage.products.list')
            ->with('products',$products)
            ->with('brands',$brands)
            ->with('conditions',isset($input['conditions']) && !empty($input['conditions']) ? json_decode($input['conditions']) : array())
            ->with('product_temp_categorys',$product_temp_categorys)
            ->with('cur',$type);
    }

    /**
     * 获取作品详情
     * @param Request $request
     * @return $this
     */
    public function getDetail($product_id){
        if(!is_numeric($product_id)){
            return redirect()->back()->with('error_message','未找到产品');
        }
        $product = $this->product_back_service->get_product_detail($product_id);
        if(empty($product)){
            return redirect()->back()->with('error_message','产品存在异常');
        }
        return view('backstage.products.detail')->with('product',$product);
    }

    /**
     * 修改产品显示状态
     * @param Request $request
     * @return mixed
     */
    public function postShow(Request $request){
        $product_id = $request->input('product_id');
        $is_show = intval($request->input('is_show'));
        if(empty($this->product_back_service->product_exist($product_id))){
            return Response::json(array('state'=>'M00001','result'=>(object)array(),'message'=>'为找到产品'));
        }
        $result = $this->product_back_service->update_product(array('product_id'=>$product_id,'is_show'=>$is_show,'updated_at'=>time()));
        return Response::json(array('state'=>'M00000','result'=>array('is_show'=>$is_show),'message'=>'修改成功'));
    }

    /**
     * 后台订单创建作品
     * @param Request $request
     * @return mixed
     */
    public function postCreateProduct(Request $request){
        $input = $request->all();
        $result = $this->product_back_service->create_product($input);
        return Response::json($result);
    }

    /**
     * 后台创建作品
     * @Author    zhouchao
     * @DateTime  2017-03-08
     * @copyright 2017 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function getCreateProductFromBackstage(Request $request, ProductTempTool $productTempTool)
    {
        $temp = $productTempTool->getProductTempsByProductTempIds([]);
        return view('backstage.products.create_product_from_backstage')->with(['temps'=>$temp]);
    }

    /**
     * 查询素材标签
     * @Author    zhouchao
     * @DateTime  2017-03-09
     * @copyright 2017  QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @param     SourcePicApiService
     * @return    mixed
     */
    public function postSearchTagList(Request $request, SourcePicApiService $SourcePicApiSer)
    {
        $product_temp_id = trim($request->input('product_temp_id'));
        $tag_list=$SourcePicApiSer->getTagList(array('per_page'=>100,'product_temp_id'=>$product_temp_id));
        return Response::json(array('state'=>'M00000','result'=>$tag_list,'message'=>'成功'));
    }

    /**
     * 根据素材标签获取素材列表
     * @Author    zhouchao
     * @DateTime  2017-03-09
     * @copyright 2017 QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @param     SourcePicApiService
     * @return    mixed
     */
    public function postSourcePicList(Request $request, SourcePicApiService $SourcePicApiSer)
    {
        $product_temp_id = trim($request->input('product_temp_id'));
        $tag_id = intval($request->input('tag_id'));
        $source_pics=$SourcePicApiSer->getSourcePicByTagId(array('per_page'=>20,'tag_id'=>$tag_id,'product_temp_id'=>$product_temp_id));
        return Response::json(array('state'=>'M00000','result'=>$source_pics,'message'=>'成功'));
    }

    /**
     * 创建作品
     * @Author    zhouchao
     * @DateTime  2017-03-09
     * @copyright 2017  QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postCreateProductFromBackstage(Request $request)
    {
        $input = $request->all();
        $result = $this->product_back_service->create_product_from_backstage($input);
        if($result['state'] != 'M00000'){
            return redirect()->back()->with('error_message',$result['message']);
        } 
        return redirect()->back()->with('success_message',$result['message']);
    }

    /**
     * 上传自定义素材
     * @Author    zhouchao
     * @DateTime  2017-03-09
     * @copyright 2017   QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postUploadSourcePicSimpleInfo(Request $request)
    {
        $rule=array(
            'file' => 'required|mimes:png,gif,jpeg,jpg,bmp'
        );
        $validator = $this->product_back_service->store($request,$rule);
        if($validator->fails()){
            if($validator->errors()->first('file')){
                $result = ['message'=>'图片格式','state'=>'M00001','data'=>[]];
            }
            return response()->json($result);
        }
        //保存自定义素材
        $image = $request->file('file');
        $result = $this->product_back_service->save_user_creatives($image,2);
        return Response::json($result);
    }

    /**
     * 更新自定义素材价格
     * @Author    zhouchao
     * @DateTime  2017-03-09
     * @copyright 2017   QuanZhouYunYiShang Inc.
     * @license   [license]
     * @version   [version]
     * @param     Request
     * @return    mixed
     */
    public function postUpdatePrice(Request $request)
    {
        $creative_id = trim($request->input('creative_id'));
        if(empty($creative_id) || !is_numeric($creative_id)){
            return ['state'=>'M00001','message'=>'请选择要修改的素材','data'=>[]];
        }
        $price = $request->input('price');
        $preferential_price = $request->input('preferential_price');
        if(is_null($price) && is_null($preferential_price)){
            return ['state'=>'M00001','message'=>'请输入要修改的价格','data'=>[]];
        }
        $user_creative = $this->product_back_service->get_user_creatives(env('MYTEE_OFFICIAL_USER_ID'),$creative_id);
        if(empty($user_creative)){
            return ['state'=>'M00001','message'=>'没有找到素材','data'=>[]];
        }
        $input['creative_id'] = $creative_id;
        $input['price'] = !is_null($price) ? doubleval($price) : $user_creative->price;
        $input['preferential_price'] = !is_null($preferential_price) ? doubleval($preferential_price) : $user_creative->preferential_price;
        $input['user_id'] = env('MYTEE_OFFICIAL_USER_ID');
        $input['updated_at'] = time();
        $result = $this->product_back_service->update_user_creatives_price($input);
        return Response::json($result);
    }
}
